package io.litchi.auth.domain.repository;

import io.litchi.auth.domain.entity.auth.User;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 *
 * 用户操作
 * @author helang
 * @since 2025-09-19
 */
public interface UserRepository extends JpaRepository<User,Long>, JpaSpecificationExecutor<User> {

    <T> Optional<T> findByUsername(String username, Class<T> clazz);

    @Modifying
    @Query("UPDATE User u SET u.lastLoginDate = :lastLoginDate WHERE u.id = :id")
    void updateLastLoginDate(@Param("id") Long id, @Param("lastLoginDate") LocalDateTime lastLoginDate);

    List<User> findByNickname(String nickname);


    List<User> findByMobile(String mobile);

    @Modifying
    @Query("UPDATE User u SET u.status = :status WHERE u.id = :id")
    void updateStatusById(@NotNull(message = "用户id不能为空") @Param("id") Long id, @NotNull(message = "用户状态不能为空") @Param(("status")) Integer status);
}